package com.hh.pms.cm.service.impl;

import com.hh.pms.cm.domain.*;
import com.hh.pms.cm.service.*;
import com.hh.pms.sae.domain.NobidNonPro;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IBsContractBusinessServiceImpl implements IBsContractBusinessService {

    @Resource
    private IBsContractService bsContractService;
    @Resource
    private IBsInventoryService inventoryService;
    @Resource
    private IBsPaymentService paymentService;
    @Resource
    private IBsSignService signService;

    @Override
    @Transactional
    public int addNoTenderBsContract(BsContract bsContract) {
        //创建合同
        bsContractService.insertBsContract(bsContract);

        Long eid = bsContract.getEid();
        NobidNonPro pro = new NobidNonPro();
        pro.setEid(eid);
        pro.setGid(bsContract.getGid());
        //添加非招标项目里的eid
        bsContractService.updateNobidNonProSid(pro);

        //添加设备信息
        List<BsInventory> list1 = bsContract.getBsInventoryList();
        if (list1 != null) {
            for (BsInventory bsInventory : list1) {
                bsInventory.setEid(eid);
                inventoryService.insertBsInventory(bsInventory);
            }
        }
        List<BsPayment> list2 = bsContract.getBsPaymentList();
        if (list2 != null) {
            for (BsPayment bsPayment : list2) {
                bsPayment.setEid(eid);
                paymentService.insertBsPayment(bsPayment);
            }
        }
        BsSign bsSign = bsContract.getBsSign();
        bsSign.setEid(eid);
        signService.insertBsSign(bsSign);

        ComPubAttachments comPubAttachments = bsContract.getComPubAttachments();
        if (comPubAttachments.getAnUrl() != null) {
            comPubAttachments.setEid(Math.toIntExact(eid));
            bsContractService.insertComPubAttachments(comPubAttachments);
        }
        return 1;
    }
}
